package com.king.tv;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.king.tv.dao.greendao.bean.User;
import com.king.tv.dao.greendao.manager.ManagerFactory;
import com.king.tv.di.component.AppComponent;
import com.king.tv.di.component.DaggerAppComponent;
import com.king.tv.di.module.AppModule;
import com.king.tv.http.HttpLoader;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;

import java.util.List;

/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/2/13
 */

public class App extends Application {
    private static App instance = null;

    public static int tabposition;

    public static final String TAG = "kingtv";

    private static final String BUGLY_ID  = "28aeafeef1";

    public static User user = null;

    private AppComponent mAppComponent;

    private HttpLoader mhttpLoader;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        instance = this;
        MultiDex.install(base);
        // 安装tinker
        Beta.installTinker();
//        Beta.installTinker();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        // 调试时，将第三个参数改为true
//        Bugly.init(this,BUGLY_ID,false);
        mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this,Constants.BASE_URL)).build();
        Bugly.init(this,"afcee60f7b",true);
//        NeverCrash.init(new NeverCrash.CrashHandler() {
//            @Override
//            public void uncaughtException(Thread t, Throwable e) {
//                CrashReport.postCatchedException(e);
//            }
//        });
    }




    public AppComponent getAppCommponent(){
        return mAppComponent;
    }


    public HttpLoader getHttpLoader(){
        return mhttpLoader;
    }

    /**
     * 懒汉模式获取appcontext对象
     * @return
     */
    public static App getInstance() {
        if (instance == null) {
            instance = new App();
        }
        return instance;
    }

    public static User getUser() {
        List<User> list = ManagerFactory.getInstance().getStudentManager().queryAll();
        if(list.size()>0){
            user = list.get(0);
            return user;
        }else{
            return user;
        }
    }
}
